﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SelectSanPhamByLoaiSP_NSX : System.Web.UI.Page
{
    private DataSet objSanPhamDS;
    private DataSet objLoaiSPDS;
    private DataSet objNSXDS;
    private DataSet objDataSet;
    private String filename;
    private String tensp;

    protected void Page_Load(object sender, EventArgs e)
    {
        PopulateControl();
        SelectTenLoaiSP_NSX();
    }

    private void SelectTenLoaiSP_NSX()
    {
        string loaisp = Request.QueryString["loaisp"];
        string nsx = Request.QueryString["nsx"];

        LoaiSanPhamBUS objLoaiSP = new LoaiSanPhamBUS(TGDDConfiguration.DbConnectionString);
        objLoaiSPDS = objLoaiSP.SelectTenLoaiSanPham(int.Parse(loaisp));
        DataRow row = objLoaiSPDS.Tables[0].Rows[0];
        lblLoaiSP.Text = row["TenLoaiSanPham"].ToString();

        NhaSanXuatBUS objNSX = new NhaSanXuatBUS(TGDDConfiguration.DbConnectionString);
        objNSXDS = objNSX.SelectTenNSX(int.Parse(nsx));
        row = objNSXDS.Tables[0].Rows[0];
        lblNSX.Text = row["TenNSX"].ToString();
    }
    private void PopulateControl()
    {
        SanPhamBUS objSanPham = new SanPhamBUS(TGDDConfiguration.DbConnectionString);
        // lay loai sp
        string loaisp = Request.QueryString["loaisp"];
        // lay nsx
        string nsx = Request.QueryString["nsx"];
        // Laytrang hien thoi
        string page = Request.QueryString["Page"];
        if (page == null)
        {
            page = "1";
        }
        int howManyPages = 1;
        objSanPhamDS = objSanPham.SelectSanPhamByLoaiSP_NSX_Paging(
            int.Parse(loaisp), int.Parse(nsx),
            int.Parse(page), TGDDConfiguration.ProductsPerPage,
            out howManyPages);
        repSanPham.DataSource =
            objSanPhamDS.Tables[0];
        repSanPham.DataBind();
        lblSoSanPham.Text = objSanPhamDS.Tables[0].Rows.Count.ToString();
        if (howManyPages > 1)
        {
            // have the current page as integer
            int currentPage = Int32.Parse(page);
            // make controls visible
            lblPaging.Visible = true;
            previousLink.Visible = true;
            nextLink.Visible = true;
            // set the paging text
            lblPaging.Text = "Page " + page + " of " + howManyPages.ToString();
            // create the Previous link
            if (currentPage == 1)
                previousLink.Enabled = false;
            else
            {
                this.previousLink.Attributes.Add("onclick", "javascript:GoToPageSanPhamByLoaiSP_NSX(" + int.Parse(loaisp) + "," + int.Parse(nsx) + "," + (currentPage - 1) + ")");
                //previousLink.NavigateUrl =
                //    Request.Url.AbsolutePath + newQueryString +
                //    "Page=" + (currentPage - 1).ToString();
            }
            // create the Next link
            if (currentPage == howManyPages)
                nextLink.Enabled = false;
            else
            {
                this.nextLink.Attributes.Add("onclick", "javascript:GoToPageSanPhamByLoaiSP_NSX(" + int.Parse(loaisp) + "," + int.Parse(nsx) + "," + (currentPage + 1) + ")");
                //nextLink.NavigateUrl =
                //    Request.Url.AbsolutePath + newQueryString +
                //    "Page=" + (currentPage + 1).ToString();
            }
        }
    }

    /*public DataSet NewHinhDataSet()
    {
        DataSet objDataSet = new DataSet();
        DataTable objDataTable = objDataSet.Tables.Add("HinhMinhHoa");

        DataColumn objDataColumn;

        objDataColumn = new DataColumn("Hinh", Type.GetType("System.String"));
        objDataTable.Columns.Add(objDataColumn);

        objDataColumn = new DataColumn("TenSanPham", Type.GetType("System.String"));
        objDataTable.Columns.Add(objDataColumn);

        objDataColumn = new DataColumn("width", Type.GetType("System.Int32"));
        objDataTable.Columns.Add(objDataColumn);

        objDataColumn = new DataColumn("height", Type.GetType("System.Int32"));
        objDataTable.Columns.Add(objDataColumn);

        return objDataSet;
    }

    protected void repSanPham_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        RepeaterItem item = e.Item;
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater repHinhMinhHoa = (Repeater)item.FindControl("repHinhMinhHoa");
            DataRowView myDataRowView = (DataRowView)item.DataItem;

            filename = myDataRowView.Row["HinhMinhHoa"].ToString();
            tensp = myDataRowView.Row["TenSanPham"].ToString();

            objDataSet = NewHinhDataSet();
            DataRow myDataRow = objDataSet.Tables["HinhMinhHoa"].NewRow();

            //lay kich thuoc image, sau do thuc hien resize
            int fileWidth = 0;
            int fileHeight = 0;
            GlobalFunction.GetImageSize(ref fileWidth, ref fileHeight, Server.MapPath(".") + "\\images\\products\\" + filename);
            
            GlobalFunction.ResizeImage(ref fileWidth, ref fileHeight, TGDDConfiguration.FixSizeList);

            if (fileHeight<fileWidth-20)
            {
                fileHeight = fileWidth-20;
            }
            else if (fileWidth < fileHeight - 20)
            {
                fileWidth = fileHeight - 20;
            }
                
            ///////////////////////////////
            myDataRow["Hinh"] = filename;
            myDataRow["TenSanPham"] = tensp;
            myDataRow["width"] = fileWidth;
            myDataRow["height"] = fileHeight;
            objDataSet.Tables["HinhMinhHoa"].Rows.Add(myDataRow);

            repHinhMinhHoa.DataSource = objDataSet.Tables[0];
            repHinhMinhHoa.DataBind();
        }
    }*/
    private void MessageBox(string msg)
    {
        Page.Controls.Add(new LiteralControl(
         "<script language='javascript'> window.alert('" + msg.Replace("'", "\\'") + "');</script>"));
    }
    protected void repSanPham_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (Session["g_UserId"] == null)
        {
            MessageBox("Vui lòng đăng nhập để mua hàng!");
            return;
        }
        // Add the product to the shopping cart
        GioHangBUS objShoppingCart =
            new GioHangBUS(TGDDConfiguration.DbConnectionString);
        GioHangDTO shoppingCartDto = new GioHangDTO();
        shoppingCartDto.Id = AccessGioHang.idGioHang;
        shoppingCartDto.IdSanPham = int.Parse(e.CommandArgument.ToString());

        try
        {
            // Execute the insert command
            bool success = objShoppingCart.AddItemToGioHang(shoppingCartDto);
        }
        catch (System.Exception ex)
        {
            throw new Exception(ex.Message, ex.InnerException);
        }
    }
}
